home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / TexturingAndModeling:AProceduralApproach / KMShaders / KMTerranBump.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  3.5 KB  |  115 lines

  1. /* modified by wave to KMTerranBump for name space protection... */
  2. /*
  3.  * terranbump.sl - displacement for an Earth-like planet.
  4.  *
  5.  *
  6.  * DESCRIPTION:
  7.  *      When put on a sphere, makes displacements to make Earth-like
  8.  *   mountains and oceans.  The shader works by using a variety of fractal 
  9.  *   turbulence and mottling techniques.
  10.  *      Note that there is a companion surface shader "terran".  You need
  11.  *   to use exactly the same parameters for "terran" and "terranbump" to
  12.  *   get them to work well together.
  13.  *
  14.  *
  15.  * PARAMETERS:
  16.  *    spectral_exp, lacunarity, octaves - control the fractal characteristics
  17.  *                of the bump pattern.
  18.  *    bump_scale - scaling of the mountains
  19.  *    multifractal - zero uses fBm noise, nonzero uses multifractal
  20.  *    dist_scale - scaling for multifractal distortion
  21.  *    offset - elevation offset
  22.  *    sea_level - obvious
  23.  *
  24.  *
  25.  * HINTS:
  26.  *       The default values for the shader assume that the planet is
  27.  *    represented by a unit sphere.  The texture space and/or parameters
  28.  *    to this shader will need to be altered if the size of your planet
  29.  *    is radically different.
  30.  *       For best results, use with the "terran" surface shader, and
  31.  *    add a cloud layer using either "planetclouds" or "venusclouds".
  32.  *
  33.  *
  34.  * AUTHOR: Ken Musgrave.
  35.  *    Conversion to Shading Language and minor modifications by Larry Gritz.
  36.  *
  37.  *
  38.  * REFERENCES:
  39.  *
  40.  *
  41.  * HISTORY:
  42.  *    ???? - original texture developed by F. Ken Musgrave.
  43.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  44.  *
  45.  * last modified 1 March 1994 by lg
  46.  */
  47.  
  48.  
  49. #ifdef BMRT
  50. #define snoise(x) (2*(noise(x)-0.5))
  51. #else
  52. /* This is because PRMAN's noise has less range than BMRT's */
  53. #define snoise(x) (2.5*(noise(x)-0.5))
  54. #endif
  55.  
  56. #define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
  57. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  58. #define N_OFFSET 0.7
  59. #define VERY_SMALL 0.0001
  60.  
  61.  
  62.  
  63. displacement
  64. KMTerranBump (float spectral_exp = 0.5;
  65.         float lacunarity = 2, octaves = 7;
  66.         float bump_scale = 0.04;
  67.         float multifractal = 0;
  68.         float dist_scale = .2;
  69.         float offset = 0;
  70.         float sea_level = 0;)
  71. {
  72.   float chaos;
  73.   point Ptexture, tp;
  74.   float l, o, a, i, weight;    /* Loop variables for fBm calc */
  75.   float bumpy;
  76.  
  77.   /* Do all shading in shader space */
  78.   Ptexture = transform ("shader", P);
  79.  
  80.   if (multifractal == 0) {    /* use a "standard" fBm bump function */
  81.       o = 1;  l = 1;  bumpy = 0;
  82.       for (i = 0;  i < octaves;  i += 1) {
  83.       bumpy += o * snoise (l * Ptexture);
  84.       l *= lacunarity;
  85.       o *= spectral_exp;
  86.         }
  87.     }
  88.   else {            /* use a "multifractal" fBm bump function */
  89.       /* get "distortion" vector, as used with clouds */
  90.       Ptexture += dist_scale * DNoise (Ptexture);
  91.       /* compute bump vector using MfBm with displaced point */
  92.       o = spectral_exp;  tp = Ptexture;
  93.       weight = abs (VLNoise (tp, 1.5));
  94.       bumpy = weight * snoise (tp);
  95.       for (i = 1;  i < octaves  &&  weight >= VERY_SMALL;  i += 1) {
  96.       tp *= lacunarity;
  97.       /* get subsequent values, weighted by previous value */
  98.       weight *= o * (N_OFFSET + snoise(tp));
  99.       weight = clamp (abs(weight), 0, 1);
  100.       bumpy += snoise(tp) * min (weight, spectral_exp);
  101.       o *= spectral_exp;
  102.     }
  103.     }
  104.  
  105.   /* get the "height" of the bump, displacing by offset */
  106.   chaos = bumpy + offset;
  107.  
  108.   /* set bump for land masses (i.e., areas above "sea level") */
  109.   if (chaos > sea_level)
  110.       P += (bump_scale * bumpy) * normalize(Ng);
  111.  
  112.   /* Recalculate the surface normal (this is where all the real magic is!) */
  113.   N = calculatenormal (P);
  114. }
  115.